package com.callable;

import java.util.Random;
import java.util.concurrent.*;

/**
 * FutureTask
 * 1.有缓存
 * 2.结果可能会阻塞，需要等待
 */
public class CallableStudy {

	public static void main(String[] args) throws Exception {

		ExecutorService executorService = Executors.newFixedThreadPool(3);

		// FutureTask适配类
		FutureTask<Integer> futureTask1 = new FutureTask<>(() -> {
			System.out.println("执行了异步  " + Thread.currentThread().getName());
			return 1;
		});
		executorService.submit(futureTask1);
		// System.out.println(submit.get());

		executorService.execute(futureTask1);

		for (int i = 0; i < 5; i++) {
			// 这里的线程不会再开线程取执行，因为结果被缓存了
			new Thread(futureTask1, "A" + i).start();
			// 获取任务返回值内容
			System.out.println(futureTask1.get());
		}
	}
}

class MyThread implements Callable<Integer> {
	static int i = 0;

	@Override
	public Integer call() throws Exception {
		System.out.println("执行了call()  " + Thread.currentThread().getName());
		i++;
		return i;
	}

}
